<#include "/templates/taglibs.ftl" />
<#include "/templates/function.ftl" />
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="${contextPath}/assets/plugins/layui/css/layui.css" media="all" />
        <style>
            p {
                margin-bottom: 10px;
                font-size: 15px;
                color: #393D49;
                font-weight: 400;
                line-height: 1.8;
            }
            .site-content {
                background-color: #fff;
                padding: 20px 0 10px 20px;
            }
            .site-h1 {
                margin-bottom: 20px;
                line-height: 60px;
                padding-bottom: 10px;
                color: #393D49;
                border-bottom: 1px solid #eee;
                font-size: 28px;
                font-weight: 400;
            }
        </style>
    </head>
    <body>
        <div class="layui-fluid layui-container">
            <div class="layui-row site-content">
                <div class="layui-col-md12">
                    <h1 class="site-h1">业务层Service</h1>
                    <p>项目中用来实现相应模块的业务功能，通过该模块的业务逻辑和算法，完成业务处理，并调用数据层提供的接口，实现业务数据的存储和读取。</p>
                    <p>在项目中写业务层一般遵循下面的规范：</p>
                    <p>1.命名：接口：服务名+Service，实现类：服务名+ServiceImpl，例如：BsExampleInfoService，BsExampleInfoServiceImpl。</p>
                    <p>2.结合应用场景，定义服务名称和接口，服务名称应能体现服务内容。</p>
                    <p>3.简单的服务可继承泛型类BaseService，该类实现了常用的服务接口。</p>
                    <p>4.业务层在实现类上增加注解@Service，使该类定义为Service服务组件类，例如：@Service(“BsExampleInfoService”)</p>
                    <p>5.服务类中，通过@Resource注解，引入要调用的数据层接口。一个服务类中，可以引入多个数据层接口。</p>
                    <pre class="layui-code" lay-skin="notepad">
@Service("BsExampleInfoService")
public class BsExampleInfoServiceImpl extends BaseServiceImpl<BsExampleInfo>
    implements BsExampleInfoService{
    
    @Resource(name = "BsExampleInfoDao")
    private BsExampleInfoDao exampleInfoDao;
}
                     </pre>
                    <p>6.在事务方法实现中，通过@Transactional完成事务处理。</p>
                    <pre class="layui-code" lay-skin="notepad">
@Transactional
public void updateExampleInfo(BsExampleInfo exampleInfo) {
    .......
}
                     </pre>
                    <p>7.合理使用缓存功能，通过cache注解，缓存需要的方法数据</p>
                     <pre class="layui-code" lay-skin="notepad">
//在查询接口上定义缓存，再次使用相同的参数调用方法，则从缓存读取，不再查库
@Cacheable(value = "adminCache", key = "'MenusService.getMenuTreeList('+#guid+','+#uid+')'")
 public List<TreeNode> getMenuTreeList(String guid, String uid) {
     List<Menus> list = findByUid(uid, guid);
     return getMenuItems(list, guid, "-1", uid);
 }
//在增加、修改、删除接口上，要清除缓存，防止读到脏数据
@CacheEvict(value = "adminCache", allEntries = true)
 public void updateMenu(Menus menus, String upperid) {
     menusDao.updateMenu(menus, upperid);
}
                     </pre>                   
                    <fieldset class="layui-elem-field layui-field-title site-title">
                        <legend><a name="get">示例代码</a></legend> 
                    </fieldset>
                    <pre class="layui-code" lay-skin="notepad">
package org.stvd.service.admin;
import java.util.List;
import org.stvd.entities.admin.UserBase;
import org.stvd.service.base.BaseService;
public interface UserBaseService extends BaseService<UserBase> {
    void updateUserBase(UserBase userBase);
    List<UserBase> findListByUser(String userid, String uname, String mobile, String email) ;
}
                    </pre>
                    <pre class="layui-code" lay-skin="notepad">
package org.stvd.service.admin.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.core.util.DateUtil;
import org.stvd.entities.admin.UserBase;
import org.stvd.entities.admin.UserLogin;
import org.stvd.entities.admin.Users;
import org.stvd.repository.admin.UserBaseDao;
import org.stvd.repository.admin.UserLoginDao;
import org.stvd.repository.admin.UsersDao;
import org.stvd.service.admin.UserBaseService;
import org.stvd.service.base.impl.BaseServiceImpl;
@Service("UserBaseService")
public class UserBaseServiceImpl extends BaseServiceImpl<UserBase> implements UserBaseService {
    @Resource(name = "UserBaseDao")
    private UserBaseDao userBaseDao;
    @Resource(name = "UsersDao")
    private UsersDao usersDao;
    @Resource(name = "UserLoginDao")
    private UserLoginDao userLoginDao;
    @Override
    @Transactional
    public void updateUserBase(UserBase userBase) {
        Users user = usersDao.findByPk(Users.class, userBase.getUser_id());
        if (user != null) {
            user.setUname(userBase.getUname());
            user.setModifydate(DateUtil.getSystemDate());
            usersDao.update(user);
            userBaseDao.update(userBase);
        }
        if(userBase.getE_mail()!=null && !"".equals(userBase.getE_mail())){
            UserLogin userLoginEmail = userLoginDao.findUserLoginByIDType(userBase.getUser_id(), "Email");
            if(userLoginEmail!=null) {
                if(!userBase.getE_mail().equals(userLoginEmail.getLoginAccount())) {
                    userLoginEmail.setLoginAccount(userBase.getE_mail());
                    userLoginDao.update(userLoginEmail);
                }
            }else {
                userLoginEmail = new UserLogin();
                userLoginEmail.setUserId(userBase.getUser_id());
                userLoginEmail.setLoginAccount(userBase.getE_mail());
                userLoginEmail.setLoginStatus("1");
                userLoginEmail.setLoginType("Email");
                userLoginDao.insert(userLoginEmail);
            }
        }else {
            UserLogin userLoginEmail = userLoginDao.findUserLoginByIDType(userBase.getUser_id(), "Email");
            if(userLoginEmail!=null) {
                userLoginDao.deleteByIdType(userBase.getUser_id(), "Email");
            }
        }
        if(userBase.getMobile()!=null && !"".equals(userBase.getMobile())) {
            UserLogin userLoginMobile = userLoginDao.findUserLoginByIDType(userBase.getUser_id(), "Mobile");
            if(userLoginMobile!=null) {
                if(!userBase.getMobile().equals(userLoginMobile.getLoginAccount())) {
                    userLoginMobile.setLoginAccount(userBase.getMobile());
                    userLoginDao.update(userLoginMobile);
                }
            }else {
                userLoginMobile = new UserLogin();
                userLoginMobile.setUserId(userBase.getUser_id());
                userLoginMobile.setLoginAccount(userBase.getMobile());
                userLoginMobile.setLoginStatus("1");
                userLoginMobile.setLoginType("Mobile");
                userLoginDao.insert(userLoginMobile);
            }
        }else {
            UserLogin userLoginMobile = userLoginDao.findUserLoginByIDType(userBase.getUser_id(), "Mobile");
            if(userLoginMobile!=null) {
                userLoginDao.deleteByIdType(userBase.getUser_id(), "Mobile");
            }
        }
    }
    public List<UserBase> findListByUser(String userid, String uname, String mobile, String email) {
        return userBaseDao.findListByUser(userid, uname, mobile, email);
    }
}
                    </pre>
                </div>
            </div>
        </div>
    </body>
    <script type="text/javascript" src="${contextPath}/assets/plugins/layui/layui.js"></script>
    <script>
        layui.use('code', function(){ 
            layui.code({
                encode: true
            }); 
          });
    </script>
</html>